/**************************************************************************//**
  \file  rMain.c

  \brief Main

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2012, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
  History:

 ******************************************************************************/
/******************************************************************************
                   Includes section
 ******************************************************************************/
#include <rTypes.h>
#include <rParser.h>
#include <rMem.h>
#include <rEvents.h>
#include <rZcl.h>
#include <leds.h>
#include <aps.h>
#include <bufferallocator.h>
#include <ZAppSiSerialManager.h>
#include <pdsDataServer.h>

/*****************************************************************************
                         Types section
******************************************************************************/
typedef void (*taskHandlers_t)(void);
/****************************************************************************
                         Static functions prototypes section
******************************************************************************/
static void rInit(void);
/******************************************************************************
                         Prototypes section
 ******************************************************************************/
void *rGetMem(void);
/******************************************************************************
                         Static variables section
 ******************************************************************************/
//static uint8_t taskPosted = 0; //Array of bits indicates which task processor requested
/*****************************************************************************
                         Global variables section
******************************************************************************/
RState_t rState = R_INIT_STATE;
//Bit position number is equal to appropriate task ID
/*
PROGMEM_DECLARE(static const taskHandlers_t taskHandlers[R_TASK_TABLE_SIZE]) = {rUsartTaskHandler,
  rParserTaskHandler};
  */
/******************************************************************************
                         Implementations section
 ******************************************************************************/
/*****************************************************************************
  Peforms APS sublayer processing
  Parameters: None
  Returns: None
 *****************************************************************************/
void APL_TaskHandler(void)
{
  if (R_INIT_STATE == rState)
  {
    rInit();
  }
  else
  {
    rParserTaskHandler();
  }
}

static void rInit(void)
{
  BSP_OpenLeds();
  BSP_OffLed(LED_RED);
  BSP_OffLed(LED_YELLOW);
  BSP_OnLed(LED_GREEN);

#ifdef _COMMISSIONING_
  if (PDS_IsAbleToRestore(BC_ALL_MEMORY_MEM_ID))
    PDS_Restore(BC_ALL_MEMORY_MEM_ID);
#endif // _COMMISSIONING_

  rState = R_IDLE_STATE;

  openBufferAllocator();

#if (APP_INTERFACE == APP_INTERFACE_USART) || (APP_INTERFACE == APP_INTERFACE_VCP) || (APP_INTERFACE == APP_INTERFACE_UART) || (APP_INTERFACE == APP_INTERFACE_USBFIFO)
  openSerialManager(INTERFACE_ID_USART0, incomingFrameHandler);
#elif (APP_INTERFACE == APP_INTERFACE_SPI)
  openSerialManager(INTERFACE_ID_SPI, incomingFrameHandler);
#else
  #error "Unknown serial interface"
#endif

  rInitEvents();

#if APP_ZCL_REQUESTS > 0
  zclReset();
#endif /* APP_ZCL_REQUESTS */
}

/***************************************************************************//**
\brief Static callback for adapter assert
\param condition[in] - condition for assert
\param code[in] - assert code
\return none
******************************************************************************/
void adapterAssert(bool condition, uint16_t code)
{
  (void)condition;
  (void)code;
  halAssert(condition, code);
}

/**********************************************************************//**
  \brief Main - C program main start function

  \param none
  \return none
**************************************************************************/
int main(void)
{
  SYS_SysInit();

  for(;;)
  {
    SYS_RunTask();
  }
}
/* eof rMain.c */
